<template>
  <div class="table-data">
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="form"
      label-width="180px"
      size="small"
      class="form-box"
    >
      <el-form-item label="权限组名称" prop="name">
        <el-input type="text" v-model="form.name" placeholder="请输入权限组名称"></el-input>
      </el-form-item>
      <!-- <el-form-item label="当前管理员的权限组id" prop="parent_id">
        <el-input type="text" v-model="form.parent_id" placeholder="请输入当前管理员的权限组id"></el-input>
      </el-form-item> -->
      <el-form-item label="备注" prop="remark">
        <el-input type="text" v-model="form.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="auths">
        <el-tree
          :data="Freedata"
          :props="defaultProps"
          check-strictly
          show-checkbox
          default-expand-all
          node-key="id"
          ref="tree"
          @check-change="handleCheckTree">
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{data.name}}</span>
          </span>
        </el-tree>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="cancel" size="small">取消</el-button>
      <el-button @click="submitFormAdd('ruleForm')" type="primary" size="small">确定</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Prop, Provide } from "vue-property-decorator";
import systemApi from "@/api/system";
@Component({
  components: {}
})
export default class AddDialog extends Vue {
  // 属性或参数中使用 ！：表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
  check:boolean = true//防重复提交
  @Provide() defaultProps:any={children: 'children',label: 'label'}
  @Provide() cheeckedKey:any=[]
  @Provide() Freedata:any= []//树形表数据
  @Provide() form:any={
    id:0,
    name:"",
    parent_id: "",
    remark:"",
    auths:""
  };
  @Provide() rules: any = {
    name: [{ required: true, message: "请输入权限组名称", trigger: "blur" }],
    remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
  };
  // 选择树形复选框时选中参数
  handleCheckTree(data, checked, indeterminate){
    this.findchildren(data, checked); 
  }
  findchildren(current,checked) {
     //详细的事件，属性等操作自己啃element-ui文档去
    (this as any).$refs.tree.setChecked(current.id, checked, true);
    let d = current.children;
    if (d.length > 0) {
      d.forEach(a => {
        this.findchildren(a,checked);
      });
    }
  }
  // 取消
  cancel(){
    this.$router.push({
      path:'/systemGroupList'
    })
  }
  // 提交
  submitFormAdd(formName: any) {
    this.form.auths=''
     const node = (this as any).$refs.tree.getCheckedNodes();
     const id:any = [];
     node.forEach(item => {
        id.push(item.id);
     });
    this.form.auths=id;
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        this.form.paparent_idd1=this.$route.query.id
        if(this.check) {
          let _this = this
          _this.check = false
          systemApi.saveSystemGroup(_this.form)
          .then((res: any) => {
            if(res.data.status){
              _this.$message({
                message: '操作成功',
                type: "success"
              });
              _this.$router.push({
                path:'/systemGroupList'
              })
              _this.$emit("loadData")
              setTimeout(function(){
                _this.check = true
              },3000)
            }else{
              _this.$message({
                message: res.data.msg,
                type: "error"
              });
              _this.check = true
            }
          });
        }
        
      }
    });
  }
  created() {
    systemApi.systemList()
    .then((res: any) => {
      let Data=res.data.data
      this.Freedata=Data
    });
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer{
  padding-left: 180px
}
.form-box {
  .el-input,
  .el-select {
    width: 200px !important;
  }
}
</style>
